﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Security.Cryptography.X509Certificates;
using System.Web;
using Projectmanagement.Entity;
using Projectmanagement.Models;
using Projectmanagement.Utl;

namespace ProjectManagement.Models
{
    public class DQ
    {
        public static List<_EmployeeLocal> GetEmployeeLocals()
        {
            var data = from x in CH.DB.EmployeeRolePMs
                       select new _EmployeeLocal()
                       {
                           ID = x.ID,
                           EmployeeID = x.EmployeeID,
                           CreatedDate = x.CreatedDate,
                           Creator = x.Creator,
                           Description = x.Description,
                           ModifiedDate = x.ModifiedDate,
                           ModifiedUser = x.ModifiedUser,
                           RolePMID = x.RolePMID,
                           Sequence = x.Sequence,
                           Name = x.Name
                       };
            var result = new List<_EmployeeLocal>();
            foreach (var x in data)
            {
                _EmployeeLocal item = x;
                result.Add(item);
            }

            return result;
        }
        public static IQueryable<_RolePM> GetRolePMs()
        {
            var data = from c in CH.DB.RolePMs
                       select new _RolePM()
                       {
                           CreatedDate = c.CreatedDate,
                           Creator = c.Creator,
                           Description = c.Description,
                           Name = c.Name,
                           RoleLVL = c.RoleLVL,
                           ID = c.ID,
                           ModifiedDate = c.ModifiedDate,
                           ModifiedUser = c.ModifiedUser,
                           Sequence = c.Sequence
                       };
            return data;
        }
        public static IQueryable<_Country> GetCountrys()
        {
            var data = from c in CH.DB.Countrys
                       select new _Country()
                       {
                           CreatedDate = c.CreatedDate,
                           Creator = c.Creator,
                           Description = c.Description,
                           Name = c.Name,
                           ID = c.ID,
                           ModifiedDate = c.ModifiedDate,
                           ModifiedUser = c.ModifiedUser,
                           Sequence = c.Sequence
                       };
            return data;
        }
        public static IQueryable<_Milestone> GetMilestone()
        {
            var data = from c in CH.DB.Milestone
                       select new _Milestone()
                       {
                           CreatedDate = c.CreatedDate,
                           Creator = c.Creator,
                           Description = c.Description,
                           MilestoneName = c.MilestoneName,
                           ID = c.ID,
                           Projectid = c.Projectid,
                           ModifiedDate = c.ModifiedDate,
                           ModifiedUser = c.ModifiedUser,
                           MilestoneTemplateID = c.MilestoneTemplateID,
                           EndDate = c.EndDate,
                           Sequence = c.Sequence,
                       };
            return data;
        }
        public static IQueryable<_MilestoneTemplate> GetMilestoneTemplate()
        {
            var data = from c in CH.DB.MilestoneTemplate
                       select new _MilestoneTemplate()
                       {
                           CreatedDate = c.CreatedDate,
                           Creator = c.Creator,
                           Description = c.Description,
                           MilestoneName = c.MilestoneName,
                           ID = c.ID,
                           Projectid = c.Projectid,
                           ModifiedDate = c.ModifiedDate,
                           ModifiedUser = c.ModifiedUser,
                           DateCount = c.DateCount,
                           EndDate = c.EndDate,
                           Sequence = c.Sequence
                       };
            return data;
        }
        public static IQueryable<_Milestone> GetMilestoneProject(int? Projectid)
        {
            var data = from c in CH.DB.Milestone
                       where Projectid == null || c.Projectid == Projectid
                       select new _Milestone()
                       {
                           CreatedDate = c.CreatedDate,
                           Creator = c.Creator,
                           Description = c.Description,
                           MilestoneName = c.MilestoneName,
                           Projectid = c.Projectid,
                           ID = c.ID,
                           ModifiedDate = c.ModifiedDate,
                           ModifiedUser = c.ModifiedUser,
                           EndDate = c.EndDate,
                           Sequence = c.Sequence
                       };
            return data;
        }
        public static IQueryable<_Motorcade> GetMotorcade()
        {
            var data = from c in CH.DB.Motorcade
                       select new _Motorcade()
                       {
                           CreatedDate = c.CreatedDate,
                           Creator = c.Creator,
                           Description = c.Description,
                           MotorcycleType = c.MotorcycleType,
                           MotorcyclePrice = c.MotorcyclePrice,
                           MotorcycleDesc = c.MotorcycleDesc,
                           TravelName = c.TravelName,
                           TravelWay = c.TravelWay,
                           TravelOtherWay = c.TravelOtherWay,
                           ComyanyName = c.ComyanyName,
                           City = c.City,

                           ID = c.ID,
                           ModifiedDate = c.ModifiedDate,
                           ModifiedUser = c.ModifiedUser,
                           Sequence = c.Sequence
                       };
            return data;
        }


        public static IQueryable<_Construct> GetConstruct()
        {
            var data = from c in CH.DB.Construct
                       select new _Construct()
                       {
                           CreatedDate = c.CreatedDate,
                           Creator = c.Creator,
                           Description = c.Description,
                           City = c.City,


                           ConstructName = c.ConstructName,
                           ConstructWay = c.ConstructWay,
                           ConstructOtherWay = c.ConstructOtherWay,
                           ConstructDesc = c.ConstructDesc,
                           ConstructPost = c.ConstructPost,
                           ComyanyName = c.ComyanyName,
                           ID = c.ID,
                           ModifiedDate = c.ModifiedDate,
                           ModifiedUser = c.ModifiedUser,
                           Sequence = c.Sequence,

                           ProjectoFivePrice = c.ProjectoFivePrice,
                           ProjectorThreePrice = c.ProjectorThreePrice,

                           ScreenFivePrice = c.ScreenFivePrice,
                           ScreenTwoPrice = c.ScreenTwoPrice,
                           ScreenZeroPrice = c.ScreenZeroPrice,
                           SoundApis = c.SoundApis,
                           SoundConsole = c.SoundConsole,
                           SoundPlatform = c.SoundPlatform,
                           SoundSic = c.SoundSic,

                           VIBackPlat = c.VIBackPlat,
                           VIBackPlatPrice = c.VIBackPlatPrice,
                           VIKT = c.VIKT,
                           VIRollUp = c.VIKT,
                           VIXback = c.VIXback
                       };
            return data;
        }
        public static IQueryable<_Tracel> GetTracel()
        {
            var data = from c in CH.DB.Tracel
                       select new _Tracel()
                       {
                           CreatedDate = c.CreatedDate,
                           Creator = c.Creator,
                           Description = c.Description,

                           TravelName = c.TravelName,
                           TravelWay = c.TravelWay,
                           TravelOtherWay = c.TravelOtherWay,
                           TravelDesc = c.TravelDesc,
                           TravelPost = c.TravelPost,
                           ComyanyName = c.ComyanyName,
                           City = c.City,

                           TravelFileId = c.TravelFileId,
                           ID = c.ID,
                           ModifiedDate = c.ModifiedDate,
                           ModifiedUser = c.ModifiedUser,
                           Sequence = c.Sequence
                       };
            return data;
        }

        public static IQueryable<_Edm> GetEdm()
        {
            var data = from c in CH.DB.Edm
                       select new _Edm()
                       {
                           CreatedDate = c.CreatedDate,
                           Creator = c.Creator,
                           Description = c.Description,
                           CompanyName = c.CompanyName,
                           ContactName = c.ContactName,
                           ContaceMobile = c.ContaceMobile,
                           ContactTitle = c.ContactTitle,
                           Quote = c.Quote,
                           Comment = c.Comment,
                           Superior = c.Superior,
                           ID = c.ID,
                           ModifiedDate = c.ModifiedDate,
                           ModifiedUser = c.ModifiedUser,
                           Sequence = c.Sequence
                       };
            return data;
        }
        public static IQueryable<_LocationDistance> GetLocationDistance(int? customerid)
        {
            var data = from c in CH.DB.LocationDistance
                       where customerid == null || c.Customerid == customerid
                       select new _LocationDistance()
                       {
                           LocationSubwayDistance = c.LocationSubwayDistance,
                           LocationStationDistance = c.LocationStationDistance,
                           LocationCityDistance = c.LocationCityDistance,
                           LocationAirportDistance = c.LocationAirportDistance,
                           CreatedDate = c.CreatedDate,
                           Creator = c.Creator,
                           Description = c.Description,
                           ID = c.ID,
                           Customerid = c.Customerid,
                           ModifiedDate = c.ModifiedDate,
                           ModifiedUser = c.ModifiedUser,
                           Sequence = c.Sequence,
                           Distance = c.Distance,
                           SpendTime = c.SpendTime,
                           TargetLocation = c.TargetLocation,
                           TrafficType = c.TrafficType
                       };
            return data;
        }


        public static IQueryable<_BanquetHall> GetBanquetHall(int? customerid)
        {
            var data = from c in CH.DB.BanquetHall
                       where customerid == null || c.Customerid == customerid
                       select new _BanquetHall()
                       {
                           Name = c.Name,
                           MarketPrice = c.MarketPrice,
                           ReferencePrice = c.ReferencePrice,
                           MeetingArea = c.MeetingArea,
                           Storey = c.Storey,
                           CreatedDate = c.CreatedDate,
                           Creator = c.Creator,
                           Description = c.Description,
                           ID = c.ID,
                           Customerid = c.Customerid,
                           ModifiedDate = c.ModifiedDate,
                           ModifiedUser = c.ModifiedUser,
                           Sequence = c.Sequence
                       };
            return data;
        }

        public static IQueryable<_Chamber> GetChamber(int? customerid)
        {
            var data = from c in CH.DB.Chamber
                       where customerid == null || c.Customerid == customerid
                       select new _Chamber()
                       {
                           ChamberArea = c.ChamberArea,
                           ChamberDesc = c.ChamberDesc,
                           ChamberFloorQuantity = c.ChamberFloorQuantity,
                           ChamberSize = c.ChamberSize,
                           ChamberStorey = c.ChamberStorey,
                           ChamberStructure = c.ChamberStructure,
                           ChamberStructureDesc = c.ChamberStructureDesc,
                           CreatedDate = c.CreatedDate,
                           Creator = c.Creator,
                           Description = c.Description,
                           PostDesc = c.PostDesc,
                           ID = c.ID,
                           Customerid = c.Customerid,
                           ModifiedDate = c.ModifiedDate,
                           ModifiedUser = c.ModifiedUser,
                           Sequence = c.Sequence,
                           MarketPrice = c.MarketPrice,
                           MeetingArea = c.MeetingArea,
                           Name = c.Name,
                           ReferencePrice = c.ReferencePrice
                       };
            return data;
        }
        public static IQueryable<_Simultaneous> GetSimultaneous()
        {
            var list = (from c in CH.DB.Simultaneous select c).ToList();
            var data = from c in CH.DB.Simultaneous
                       select new _Simultaneous()
                       {
                           CreatedDate = c.CreatedDate,
                           Creator = c.Creator,
                           Description = c.Description,
                           CompanyName = c.CompanyName,
                           ContactName = c.ContactName,
                           ContaceMobile = c.ContaceMobile,
                           ContactTitle = c.ContactTitle,
                           Translatione = c.Translatione,
                           TranslationePrice = c.TranslationePrice,
                           InterTranslationeCEDay = c.InterTranslationeCEDay,
                           InterTranslationeCEPrice = c.InterTranslationeCEPrice,
                           InterTranslationeCJDay = c.InterTranslationeCJDay,
                           InterTranslationeCJPrice = c.InterTranslationeCJPrice,
                           InterTranslationeCKDay = c.InterTranslationeCKDay,
                           InterTranslationeCKPrice = c.InterTranslationeCKPrice,
                           ConTranslationeCEDay = c.ConTranslationeCEDay,
                           ConTranslationeCEPrice = c.ConTranslationeCEPrice,
                           ConTranslationeCJDay = c.ConTranslationeCJDay,
                           ConTranslationeCJPrice = c.ConTranslationeCJPrice,
                           ConTranslationeCKDay = c.ConTranslationeCKDay,
                           ConTranslationeCKPrice = c.ConTranslationeCKPrice,
                           KeepTranslationeCEDay = c.KeepTranslationeCEDay,
                           KeepTranslationeCEPrice = c.KeepTranslationeCEPrice,
                           KeepTranslationeCJDay = c.KeepTranslationeCJDay,
                           KeepTranslationeCJPrice = c.KeepTranslationeCJPrice,
                           KeepTranslationeCKDay = c.KeepTranslationeCKDay,
                           KeepTranslationeCKPrice = c.KeepTranslationeCKPrice,
                           City = c.City,
                           Address = c.Address,
                           HostSystemPrice = c.HostSystemPrice,
                           ReceiverPrice = c.ReceiverPrice,
                           ReceiverType = c.ReceiverType,

                           Superior = c.Superior,
                           ID = c.ID,
                           ModifiedDate = c.ModifiedDate,
                           ModifiedUser = c.ModifiedUser,
                           Sequence = c.Sequence
                       };
            return data;
        }
        public static IQueryable<_Equipment> GetEquipment()
        {
            var list = (from c in CH.DB.Equipment select c).ToList();
            var data = from c in CH.DB.Equipment
                       select new _Equipment()
                       {
                           CreatedDate = c.CreatedDate,
                           Creator = c.Creator,
                           Description = c.Description,
                           CompanyName = c.CompanyName,
                           ContactName = c.ContactName,
                           ContaceMobile = c.ContaceMobile,
                           ContactEmail = c.ContactEmail,
                           ContactTitle = c.ContactTitle,
                           HostSystemPrice = c.HostSystemPrice,
                           ReceiverPrice = c.ReceiverPrice,
                           ReceiverType = c.ReceiverType,
                           ProjectorThreePrice = c.ProjectorThreePrice,
                           ProjectoFivePrice = c.ProjectoFivePrice,
                           ScreenZeroPrice = c.ScreenZeroPrice,
                           ScreenTwoPrice = c.ScreenTwoPrice,
                           ScreenFivePrice = c.ScreenFivePrice,
                           SoundConsole = c.SoundConsole,
                           SoundPlatform = c.SoundPlatform,
                           SoundSic = c.SoundSic,
                           SoundApis = c.SoundApis,
                           VIBackPlat = c.VIBackPlat,
                           VIXback = c.VIXback,
                           VIRollUp = c.VIRollUp,
                           VIKT = c.VIKT,
                           Superior = c.Superior,
                           ID = c.ID,
                           ModifiedDate = c.ModifiedDate,
                           ModifiedUser = c.ModifiedUser,
                           Sequence = c.Sequence
                       };
            return data;
        }
        public static IQueryable<_Printing> GetPrinting()
        {
            var data = from c in CH.DB.Printing
                       select new _Printing()
                       {
                           ContactName = c.ContactName,
                           ContaceMobile = c.ContaceMobile,
                           ContactTitle = c.ContactTitle,
                           CreatedDate = c.CreatedDate,
                           Creator = c.Creator,
                           Description = c.Description,
                           FiveArtPaper = c.FiveArtPaper,
                           FiveOffsetPaper = c.FiveOffsetPaper,
                           FiveBluePaper = c.FiveBluePaper,
                           FourArPaper = c.FourArPaper,
                           FourOffsetPaper = c.FourOffsetPaper,
                           FourBluePaper = c.FourBluePaper,
                           ThreeArtPaper = c.ThreeArtPaper,
                           ThreeOffsetPaper = c.ThreeOffsetPaper,
                           ThreeBluePaper = c.ThreeBluePaper,
                           WhiteCardboard = c.WhiteCardboard,
                           CardSize = c.CardSize,
                           CardAll = c.CardAll,
                           Comment = c.Comment,
                           ID = c.ID,
                           ModifiedDate = c.ModifiedDate,
                           ModifiedUser = c.ModifiedUser,
                           Sequence = c.Sequence,
                           ComyanyName = c.ComyanyName,
                           Address = c.Address
                       };
            return data;
        }
        public static IQueryable<_Customer> GetCustomer()
        {
            var list = (from c in CH.DB.Customer select c).ToList();
            var data = from c in CH.DB.Customer
                       select new _Customer()
                       {
                           CreatedDate = c.CreatedDate,
                           Creator = c.Creator,
                           ID = c.ID,
                           Description = c.Description,
                           ModifiedDate = c.ModifiedDate,
                           ModifiedUser = c.ModifiedUser,
                           Sequence = c.Sequence,
                           CustomerName = c.CustomerName,
                           ContactName = c.ContactName,
                           ContactPlane = c.ContactPlane,
                           ContactMobile = c.ContactMobile,
                           ContactEmail = c.ContactEmail,
                           ContactTitle = c.ContactTitle,
                           SuperiorName = c.SuperiorName,
                           SuperiorTitle = c.SuperiorTitle,
                           ManagerialBody = c.ManagerialBody,
                           StarLevel = c.StarLevel,
                           Country = c.Country,
                           City = c.City,
                           CustomerAddress = c.CustomerAddress,
                           AdministrativeRegion = c.AdministrativeRegion,
                           DescCreatedDate = c.DescCreatedDate,
                           DescUpdateDate = c.DescUpdateDate,
                           DescSoundEquipment = c.DescSoundEquipment,
                           DescJK = c.DescJK,
                           DescDegree = c.DescDegree,
                           RoomCount = c.RoomCount,
                           LocationCityDistance = c.LocationCityDistance,
                           LocationAirportDistance = c.LocationAirportDistance,
                           LocationStationDistance = c.LocationStationDistance,
                           LocationSubwayDistance = c.LocationSubwayDistance,
                           ChamberStructure = c.ChamberStructure,
                           ChamberFloorQuantity = c.ChamberFloorQuantity,
                           ChamberDesc = c.ChamberDesc,
                           ChamberStorey = c.ChamberStorey,
                           ChamberSize = c.ChamberSize,
                           ChamberArea = c.ChamberArea,
                           QuoteMeetPrice = c.QuoteMeetPrice,
                           QuoteMeetPriceDesc = c.QuoteMeetPriceDesc,
                           QuoteMeetFial = c.QuoteMeetFial,
                           QuoteMeetDesc = c.QuoteMeetDesc,
                           QuoteMeetGarantDesc = c.QuoteMeetGarantDesc,
                           QuoteMeetPeople = c.QuoteMeetPeople,
                           QuoteRoomPrice = c.QuoteRoomPrice,
                           QuoteRoomPriceDesc = c.QuoteRoomPriceDesc,
                           QuoteLunchMaxPeople = c.QuoteLunchMaxPeople,
                           QuoteRoomFinal = c.QuoteRoomFinal,
                           QuoteRoomDesc = c.QuoteRoomDesc,
                           QuoteLunchPrice = c.QuoteLunchPrice,
                           QuoteLunchPriceDesc = c.QuoteLunchPriceDesc,
                           QuoteLunchFinal = c.QuoteLunchFinal,
                           QuoteLunchDesc = c.QuoteLunchDesc,
                           QuoteTeaPrice = c.QuoteTeaPrice,
                           QuoteTeaPriceDesc = c.QuoteTeaPriceDesc,
                           QuoteTeaFinal = c.QuoteTeaFinal,
                           QuoteTeaDesc = c.QuoteTeaDesc,
                           RoomStane = c.RoomStane,
                           RoomLarge = c.RoomLarge,
                           RoomAdminLarge = c.RoomAdminLarge,
                           RoomAdmin = c.RoomAdmin,
                           RoomSuite = c.RoomSuite,
                           RoomPercent = c.RoomPercent,
                           RoomPrice = c.RoomPrice,
                           RoomDesc = c.RoomDesc,
                           RoomFinalPrice = c.RoomFinalPrice,
                           RoomFinalPriceDesc = c.RoomFinalPriceDesc,
                           CarModelPrice = c.CarModelPrice,
                           CarOrNot = c.CarOrNot,
                           CarPlace = c.CarPlace,
                           OtherCock = c.OtherCock,
                           OtherWesternFood = c.OtherWesternFood,
                           OtherCenterFood = c.OtherCenterFood,
                           OtherNote = c.OtherNote,
                           CommentCost = c.CommentCost,
                           CommentVirtue = c.CommentVirtue,
                           CommentDefect = c.CommentDefect,
                           CommentCate = c.CommentCate,
                           CustomerFileId = c.CustomerFileId,
                           ChamberStructureDesc = c.ChamberStructureDesc,
                           PostDesc = c.PostDesc
                       };
            return data;
        }

        public static IQueryable<_Project> GetProjects()
        {
            var data = from c in CH.DB.Projects select c;
            if (Employee.CurrentRoleLVL == RoleLevel.Manager)
            {
                data = from p in data where p.Manager == Employee.CurrentUserName select p;
            }
            else if (Employee.CurrentRoleLVL < RoleLevel.Manager)
            {
                data = from p in data where p.Members.Any(a => a.Name == Employee.CurrentUserName) select p;
            }

            if (Employee.CurrentRoleLVL <= RoleLevel.Manager)
            {
                data = data.Where(m => m.IsActived);
            }
            var s = from c in data
                    select new _Project()
                        {
                            CreatedDate = c.CreatedDate,
                            Creator = c.Creator,
                            Description = c.Description,
                            Name = c.Name,
                            ID = c.ID,
                            ModifiedDate = c.ModifiedDate,
                            ModifiedUser = c.ModifiedUser,
                            Sequence = c.Sequence,
                            AreaID = c.AreaID,
                            Conference = c.Conference,
                            ConferenceEndDate = c.ConferenceEndDate,
                            ConferenceStartDate = c.ConferenceStartDate,
                            Location = c.Location,
                            Manager = c.Manager,
                            EndDate = c.EndDate,
                            Market = c.Market,
                            IsActived = c.IsActived,
                            Product = c.Product,
                            ProjectDisplayName = c.ProjectDisplayName,
                            StartDate = c.StartDate,
                            Target = c.Target,
                            ProjectTypeID = c.ProjectTypeID,
                            ProjectCode = c.ProjectCode,
                            TeamLeader = c.TeamLeader,
                            Others = c.Others
                        };
            return s;
        }

        public static IQueryable<_Member> GetMembers(int? conferenceid)
        {
            var data = (from c in CH.DB.Members
                        where conferenceid == null || c.ProjectID == conferenceid
                        select new _Member()
                        {
                            CreatedDate = c.CreatedDate,
                            Creator = c.Creator,
                            Description = c.Description,
                            Name = c.Name,
                            ID = c.ID,
                            ModifiedDate = c.ModifiedDate,
                            ModifiedUser = c.ModifiedUser,
                            Sequence = c.Sequence,
                            IsActivated = c.IsActivated,
                            ProjectID = c.ProjectID,
                            RolePMID = c.RolePMID,
                            RolePM = c.RolePM != null ? c.RolePM.Name : string.Empty
                        });

            return data;
        }

        public static IQueryable<_City> GetCity(int? countryid)
        {
            var data = (from c in CH.DB.Citys
                        where countryid == null || c.CountryID == countryid
                        select new _City()
                        {
                            CreatedDate = c.CreatedDate,
                            Creator = c.Creator,
                            Description = c.Description,
                            ID = c.ID,
                            ModifiedDate = c.ModifiedDate,
                            ModifiedUser = c.ModifiedUser,
                            Sequence = c.Sequence,
                            Name = c.Name,
                            CountryID = c.CountryID
                        }
                      );

            return data;
        }

        public static IEnumerable<_Task> GetSubordinateTasksSearch(int? projectid, string member, bool? needConfirmonly = false, int? taskstate = null, string taskName = null)
        {

            IQueryable<string> members = null;
            IQueryable<int> ps = null;
            if (projectid == null)//如果有projectid 就是指定projectid项目的任务，如果projectid为null 就是所有参与的项目
            {
                ps = Helpers.GetUserProjects().Select(s => s.ID);
                members = from m in CH.DB.Members where ps.Contains((int)m.ProjectID) select m.Name;
            }
            else
            {
                ps = from p in CH.DB.Projects where p.ID == projectid select p.ID;
                members = from m in CH.DB.Members where projectid == m.ProjectID select m.Name;
            }

            IQueryable<Task> ts = from c in CH.DB.Tasks select c;

            if (needConfirmonly == true)
            {
                ts = ts.Where(w => w.TaskState.Code == 90);
            }
            if (!string.IsNullOrWhiteSpace(taskName))
            {
                ts = ts.Where(w => w.Name != null && w.Name.Contains(taskName));
            }
            if (!string.IsNullOrEmpty(member) && member != "-请选择-")
            {
                ts = ts.Where(w => w.Owners.Contains(member));
            }

            if (Employee.CurrentRoleLVL != RoleLevel.Administrator)
            {
                ts = ts.Where(s => s.Project.IsActived);
            }

            var task = from c in ts
                       where ps.Contains((int)c.ProjectID)
                       select new _Task()
                       {
                           ProjectCode = c.Project.ProjectCode,
                           CreatedDate = c.CreatedDate,
                           Creator = c.Creator,
                           Description = c.Description,
                           ID = c.ID,
                           ModifiedDate = c.ModifiedDate,
                           ModifiedUser = c.ModifiedUser,
                           Sequence = c.Sequence,
                           //Complement = c.Complement,
                           EndDate = c.EndDate,
                           Name = c.Name,
                           Owners = c.Owners,
                           ProjectID = c.ProjectID,
                           StartDate = c.StartDate,
                           Content = c.Content,
                           KMLinks = c.KMLinks,
                           TaskStateCode = c.TaskState != null ? c.TaskState.Code : 0,
                           Deliverables = c.Deliverables,
                           TaskStateID = c.TaskStateID,
                           FinishDate = c.FinishDate,
                           Assinger = c.Assinger,
                           TaskStateName = c.TaskState != null ? c.TaskState.Name : string.Empty,
                           TaskCode = c.TaskCode
                       };

            IEnumerable<_Task> result = null;
            result = task.AsEnumerable();

            return result;
        }

        public static IEnumerable<_Task> GetMyTasks(int? projectid, bool? allstatetask = false, string taskName = "")
        {
            IQueryable<Task> tasks = from c in CH.DB.Tasks select c;
            IQueryable<_Task> results;
            if (projectid != null && projectid != 0)
                tasks = from c in tasks where c.ProjectID == projectid select c;

            if (allstatetask != true)
            {
                tasks = from w in tasks where w.TaskState.Code != _Task.Fail && w.TaskState.Code != _Task.Pass && w.TaskState.Code != _Task.Cancel select w;
            }

            if (!string.IsNullOrWhiteSpace(taskName))
            {
                tasks = tasks.Where(w => w.Name != null && w.Name.Contains(taskName));
            }

            if (Employee.CurrentRoleLVL != RoleLevel.Administrator)
            {
                tasks = tasks.Where(s => s.Project.IsActived);
            }
            results = from c in tasks

                      select new _Task()
                      {
                          ProjectCode = c.Project.ProjectCode,
                          CreatedDate = c.CreatedDate,
                          Creator = c.Creator,
                          Description = c.Description,
                          ID = c.ID,
                          ModifiedDate = c.ModifiedDate,
                          ModifiedUser = c.ModifiedUser,
                          Sequence = c.Sequence,
                          //Complement = c.Complement,
                          EndDate = c.EndDate,
                          Name = c.Name,
                          Owners = c.Owners,
                          ProjectID = c.ProjectID,
                          StartDate = c.StartDate,
                          Content = c.Content,
                          KMLinks = c.KMLinks,
                          TaskStateCode = c.TaskState != null ? c.TaskState.Code : 0,
                          Deliverables = c.Deliverables,
                          TaskStateID = c.TaskStateID,
                          FinishDate = c.FinishDate,
                          Assinger = c.Assinger,
                          TaskStateName = c.TaskState != null ? c.TaskState.Name : string.Empty,
                          TaskCode = c.TaskCode
                      };

            var list = results.Where(w => w.Owners.Contains(Employee.CurrentUserName)).Select(s => s);
            return list;
        }
        public static IEnumerable<_Task> GetTasksMilestone(int? projectid, int? milestoneTemplateID, string member, int serachValue, int ddlTaskState, string searchProject, bool flag, string taskName = "")
        {

            IEnumerable<Task> result = null;
            var data = from c in CH.DB.Tasks select c;

            if (projectid >= 0 || milestoneTemplateID >= 0)
            {
                data = from c in data where c.MilestoneID == projectid || c.MilestoneID == milestoneTemplateID select c;
            }
            result = data.ToList();
            if (!string.IsNullOrEmpty(member))
            {
                result = result.Where(w => w.Owners.Split(new string[] { ";" }, StringSplitOptions.RemoveEmptyEntries).Any(a => a == member)).Select(s => s);
            }
            if (!string.IsNullOrWhiteSpace(taskName))
            {
                result = result.Where(w => w.TaskCode != null && w.Name.Contains(taskName));
            }


            var results = from c in result

                          select new _Task()
                          {
                              CreatedDate = c.CreatedDate,
                              Creator = c.Creator,
                              Description = c.Description,
                              ID = c.ID,
                              ModifiedDate = c.ModifiedDate,
                              ModifiedUser = c.ModifiedUser,
                              Sequence = c.Sequence,
                              //Complement = c.Complement,
                              EndDate = c.EndDate,
                              Name = c.Name.Length > 30 ? (c.Name.Substring(0, 30) + "......") : c.Name,
                              Owners = c.Owners,
                              ProjectID = c.ProjectID,
                              ProjectCode = c.Project.ProjectCode,
                              StartDate = c.StartDate,
                              Content = c.Content,
                              KMLinks = c.KMLinks,
                              TaskStateCode = c.TaskState != null ? c.TaskState.Code : 0,
                              Deliverables = c.Deliverables,
                              TaskStateID = c.TaskStateID,
                              FinishDate = c.FinishDate,
                              Assinger = c.Assinger,
                              TaskStateName = c.TaskState != null ? c.TaskState.Name : string.Empty,
                              TaskCode = c.TaskCode

                              //_TaskComments = (from tc in CH.DB.TaskComments where tc.TaskID == c.ID
                              //                 select new _TaskComment()
                              //                 {
                              //                     Submitter = tc.Submitter,
                              //                     CommentDate = tc.CommentDate,
                              //                     TaskID = tc.TaskID,
                              //                     Contents = tc.Contents
                              //                 })
                          };
            //var result = new List<_Task>();
            //foreach (var x in data)
            //{
            //    _Task item = x;
            //    item.Ttype = GetTaskType(item);
            //    result.Add(item);
            //}

            return results;

        }
        public static IEnumerable<_Task> GetTasksSearch(int? projectid, string member, int serachValue, int ddlTaskState, string searchProject, bool flag, string taskName = "")
        {
            if (flag == true)
            {
                IEnumerable<Task> result = null;
                var data = from c in CH.DB.Tasks select c;

                if (projectid >= 0)
                {
                    data = from c in data where c.ProjectID == projectid select c;
                }
                result = data.ToList();
                if (!string.IsNullOrEmpty(member))
                {
                    result = result.Where(w => w.Owners.Split(new string[] { ";" }, StringSplitOptions.RemoveEmptyEntries).Any(a => a == member)).Select(s => s);
                }
                if (!string.IsNullOrWhiteSpace(taskName))
                {
                    result = result.Where(w => w.Name != null && w.Name.Contains(taskName));
                }

                if (Employee.CurrentRoleLVL != RoleLevel.Administrator)
                {
                    result = result.Where(s => s.Project.IsActived);
                }


                var results = from c in result

                              select new _Task()
                              {
                                  CreatedDate = c.CreatedDate,
                                  Creator = c.Creator,
                                  Description = c.Description,
                                  ID = c.ID,
                                  ModifiedDate = c.ModifiedDate,
                                  ModifiedUser = c.ModifiedUser,
                                  Sequence = c.Sequence,
                                  //Complement = c.Complement,
                                  EndDate = c.EndDate,
                                  Name = c.Name.Length > 30 ? (c.Name.Substring(0, 30) + "......") : c.Name,
                                  Owners = c.Owners,
                                  ProjectID = c.ProjectID,
                                  ProjectCode = c.Project.ProjectCode,
                                  StartDate = c.StartDate,
                                  Content = c.Content,
                                  KMLinks = c.KMLinks,
                                  TaskStateCode = c.TaskState != null ? c.TaskState.Code : 0,
                                  Deliverables = c.Deliverables,
                                  TaskStateID = c.TaskStateID,
                                  FinishDate = c.FinishDate,
                                  Assinger = c.Assinger,
                                  TaskStateName = c.TaskState != null ? c.TaskState.Name : string.Empty,
                                  TaskCode = c.TaskCode

                                  //_TaskComments = (from tc in CH.DB.TaskComments where tc.TaskID == c.ID
                                  //                 select new _TaskComment()
                                  //                 {
                                  //                     Submitter = tc.Submitter,
                                  //                     CommentDate = tc.CommentDate,
                                  //                     TaskID = tc.TaskID,
                                  //                     Contents = tc.Contents
                                  //                 })
                              };
                //var result = new List<_Task>();
                //foreach (var x in data)
                //{
                //    _Task item = x;
                //    item.Ttype = GetTaskType(item);
                //    result.Add(item);
                //}

                return results.OrderByDescending(s => s.TaskStateID).ThenBy(s => s.EndDate);
            }
            else
            {
                if (serachValue != 0)
                {
                    string[] names = searchProject.Split(';');
                    var data = (from c in CH.DB.Tasks
                                join p in CH.DB.Projects on c.ProjectID equals p.ID
                                join m in CH.DB.Members on c.ProjectID equals m.ProjectID
                                where (projectid == null || c.ProjectID == projectid) && (m.ID == serachValue) && (c.TaskStateID == ddlTaskState) && (names.Contains(p.Name))
                                select new _Task()
                                {
                                    CreatedDate = c.CreatedDate,
                                    Creator = c.Creator,
                                    Description = c.Description,
                                    ID = c.ID,
                                    ModifiedDate = c.ModifiedDate,
                                    ModifiedUser = c.ModifiedUser,
                                    Sequence = c.Sequence,
                                    //Complement = c.Complement,
                                    EndDate = c.EndDate,
                                    Name = c.Name,
                                    Owners = c.Owners,
                                    ProjectID = c.ProjectID,
                                    StartDate = c.StartDate,
                                    Content = c.Content,
                                    KMLinks = c.KMLinks,
                                    TaskStateCode = c.TaskState != null ? c.TaskState.Code : 0,
                                    Deliverables = c.Deliverables,
                                    TaskStateID = c.TaskStateID,
                                    FinishDate = c.FinishDate,
                                    Assinger = c.Assinger,
                                    TaskStateName = c.TaskState != null ? c.TaskState.Name : string.Empty
                                    //_TaskComments = (from tc in CH.DB.TaskComments where tc.TaskID == c.ID
                                    //                 select new _TaskComment()
                                    //                 {
                                    //                     Submitter = tc.Submitter,
                                    //                     CommentDate = tc.CommentDate,
                                    //                     TaskID = tc.TaskID,
                                    //                     Contents = tc.Contents
                                    //                 })
                                }).Distinct();
                    //var result = new List<_Task>();
                    //foreach (var x in data)
                    //{
                    //    _Task item = x;
                    //    item.Ttype = GetTaskType(item);
                    //    result.Add(item);
                    //}

                    return data.OrderByDescending(s => s.TaskStateID).ThenBy(s => s.EndDate);
                }
                else
                {
                    string[] names = searchProject.Split(';');
                    var data = (from c in CH.DB.Tasks
                                join p in CH.DB.Projects on c.ProjectID equals p.ID
                                join m in CH.DB.Members on c.ProjectID equals m.ProjectID
                                where (projectid == null || c.ProjectID == projectid) && (c.TaskStateID == ddlTaskState) && (names.Contains(p.Name))
                                select new _Task()
                                {
                                    CreatedDate = c.CreatedDate,
                                    Creator = c.Creator,
                                    Description = c.Description,
                                    ID = c.ID,
                                    ModifiedDate = c.ModifiedDate,
                                    ModifiedUser = c.ModifiedUser,
                                    Sequence = c.Sequence,
                                    //Complement = c.Complement,
                                    EndDate = c.EndDate,
                                    Name = c.Name,
                                    Owners = c.Owners,
                                    ProjectID = c.ProjectID,
                                    StartDate = c.StartDate,
                                    Content = c.Content,
                                    KMLinks = c.KMLinks,
                                    TaskStateCode = c.TaskState != null ? c.TaskState.Code : 0,
                                    Deliverables = c.Deliverables,
                                    TaskStateID = c.TaskStateID,
                                    FinishDate = c.FinishDate,
                                    Assinger = c.Assinger,
                                    TaskStateName = c.TaskState != null ? c.TaskState.Name : string.Empty
                                    //_TaskComments = (from tc in CH.DB.TaskComments where tc.TaskID == c.ID
                                    //                 select new _TaskComment()
                                    //                 {
                                    //                     Submitter = tc.Submitter,
                                    //                     CommentDate = tc.CommentDate,
                                    //                     TaskID = tc.TaskID,
                                    //                     Contents = tc.Contents
                                    //                 })
                                }).Distinct();
                    //var result = new List<_Task>();
                    //foreach (var x in data)
                    //{
                    //    _Task item = x;
                    //    item.Ttype = GetTaskType(item);
                    //    result.Add(item);
                    //}

                    return data.OrderByDescending(s => s.TaskStateID).ThenBy(s => s.EndDate);
                }
            }
        }

        public static IQueryable<_TaskTemplate> GetTaskTemplatesSearch(int? searchRole, string searchName, int? milestoneID)
        {
            var temps = from t in CH.DB.TaskTemplates select t;

            if (searchRole != null)
            {
                temps = from t in CH.DB.TaskTemplates where searchRole == t.RolePM.RoleLVL select t;
            }

            if (!string.IsNullOrEmpty(searchName))
            {
                temps = from t in CH.DB.TaskTemplates where t.Name.Contains(searchName) select t;
            }
            if (milestoneID != null)
            {
                temps = from t in CH.DB.TaskTemplates where t.MilestoneID == milestoneID select t;
            }

            var data = from c in temps
                       select
                           new _TaskTemplate()
                           {
                               DateCount = c.DateCount,
                               CreatedDate = c.CreatedDate,
                               Creator = c.Creator,
                               Description = c.Description,
                               ID = c.ID,
                               ModifiedDate = c.ModifiedDate,
                               ModifiedUser = c.ModifiedUser,
                               Sequence = c.Sequence,
                               Name = c.Name,
                               Content = c.Content,
                               KMLinks = c.KMLinks,
                               MilestoneID = c.MilestoneID,
                               Deliverables = c.Deliverables,
                               IsDefault = c.IsDefault,
                               RolePM = c.RolePM.Name,
                               RolePMID = c.RolePMID,
                               Duration = c.Duration
                           };
            return data.OrderBy(o => o.DateCount);
        }

        public static IQueryable<_ProjectType> GetProjectTypes()
        {
            var data = from c in CH.DB.ProjectTypes
                       select
                           new _ProjectType()
                           {
                               CreatedDate = c.CreatedDate,
                               Creator = c.Creator,
                               Description = c.Description,
                               Name = c.Name,
                               ID = c.ID,
                               ModifiedDate = c.ModifiedDate,
                               ModifiedUser = c.ModifiedUser,
                               Sequence = c.Sequence
                           };
            return data;
        }

        public static IQueryable<_TaskState> GetTaskStates()
        {

            var data = from c in CH.DB.TaskStates
                       select
                           new _TaskState()
                           {
                               CreatedDate = c.CreatedDate,
                               Creator = c.Creator,
                               Description = c.Description,
                               Name = c.Name,
                               Code = c.Code,
                               ID = c.ID,
                               ModifiedDate = c.ModifiedDate,
                               ModifiedUser = c.ModifiedUser,
                               Sequence = c.Sequence
                           };
            return data;
        }

        public static IEnumerable<_ProjectTaskSchedule> GetProjectTaskSchedule(int? projectid, string member, bool flag)
        {

            var queryTemp = from t in CH.DB.Tasks
                            group t by t.ProjectID into m
                            select new
                                  {
                                      Name = m.Key,
                                      未启动 = m.Count(n => n.TaskStateID == 3),
                                      进行中 = m.Count(n => n.TaskStateID == 4),
                                      暂停 = m.Count(n => n.TaskStateID == 5),
                                      取消 = m.Count(n => n.TaskStateID == 6),
                                      通过 = m.Count(n => n.TaskStateID == 8),
                                      求助 = m.Count(n => n.TaskStateID == 9),
                                      请审核 = m.Count(n => n.TaskStateID == 10),
                                      失败 = m.Count(n => n.TaskStateID == 11)
                                  };
            var testt = queryTemp.ToList();

            var query = from p in CH.DB.Projects
                        //join m in CH.DB.Members on p.ID equals m.ProjectID
                        join q in queryTemp on p.ID equals q.Name into temp
                        from q in temp.DefaultIfEmpty()
                        select new _ProjectTaskSchedule()
                        {
                            ProjectID = p.ID,
                            ProjectCode = p.ProjectCode,
                            PMName = p.Manager,
                            NotStarted = q.未启动,
                            Underway = q.进行中,
                            Suspend = q.暂停,
                            Cancel = q.取消,
                            Pass = q.通过,
                            SeekHelp = q.求助,
                            PleaseExamine = q.请审核,
                            Faield = q.失败
                        };
            if (projectid >= 0)
            {
                query = query.Where(n => n.ProjectID == projectid);
            }
            if (!string.IsNullOrEmpty(member))
            {
                query = query.Where(w => w.PMName == member).Select(s => s);
            }
            var test = query.Where(n => n.NotStarted != null && n.Underway != null && n.Suspend != null && n.Cancel != null && n.Pass != null
                && n.SeekHelp != null && n.PleaseExamine != null && n.Faield != null);
            return test;




        }

        public static IEnumerable<_ProjectMemberSchedule> GetProjectMemberSchedule(int? projectid, string member)
        {
            //var queryTemp = from t in CH.DB.Tasks
            //                group t by t.Owners into m
            //                select new
            //                {
            //                    Name = m.Key,
            //                    未启动 = m.Count(n => n.TaskStateID == 3),
            //                    进行中 = m.Count(n => n.TaskStateID == 4),
            //                    暂停 = m.Count(n => n.TaskStateID == 5),
            //                    取消 = m.Count(n => n.TaskStateID == 6),
            //                    通过 = m.Count(n => n.TaskStateID == 8),
            //                    求助 = m.Count(n => n.TaskStateID == 9),
            //                    请审核 = m.Count(n => n.TaskStateID == 10),
            //                    失败 = m.Count(n => n.TaskStateID == 11)
            //                };

            //var ll = queryTemp.ToList();
            //var query = from p in CH.DB.Projects
            //            join m in CH.DB.Members on p.ID equals m.ProjectID
            //            join q in queryTemp on m.Name equals q.Name into temp
            //            from q in temp.DefaultIfEmpty()

            //            select new _ProjectMemberSchedule()
            //            {
            //                ProjectID = p.ID,
            //                ProjectCode = p.ProjectCode,
            //                Name = m.Name,
            //                NotStarted = q.未启动,
            //                Underway = q.进行中,
            //                Suspend = q.暂停,
            //                Cancel = q.取消,
            //                Pass = q.通过,
            //                SeekHelp = q.求助,
            //                PleaseExamine = q.请审核,
            //                Faield = q.失败
            //            };
            var queryTemp = from t in CH.DB.Tasks
                            group t by t.ProjectID into m
                            select new
                            {
                                Name = m.Key,
                                未启动 = m.Count(n => n.TaskStateID == 3),
                                进行中 = m.Count(n => n.TaskStateID == 4),
                                暂停 = m.Count(n => n.TaskStateID == 5),
                                取消 = m.Count(n => n.TaskStateID == 6),
                                通过 = m.Count(n => n.TaskStateID == 8),
                                求助 = m.Count(n => n.TaskStateID == 9),
                                请审核 = m.Count(n => n.TaskStateID == 10),
                                失败 = m.Count(n => n.TaskStateID == 11)
                            };
            var testt = queryTemp.ToList();

            var query = from p in CH.DB.Projects
                        //join m in CH.DB.Members on p.ID equals m.ProjectID
                        join q in queryTemp on p.ID equals q.Name into temp
                        from q in temp.DefaultIfEmpty()
                        select new _ProjectMemberSchedule()
                        {
                            ProjectID = p.ID,
                            ProjectCode = p.ProjectCode,
                            Name = p.Manager,
                            NotStarted = q.未启动,
                            Underway = q.进行中,
                            Suspend = q.暂停,
                            Cancel = q.取消,
                            Pass = q.通过,
                            SeekHelp = q.求助,
                            PleaseExamine = q.请审核,
                            Faield = q.失败
                        };
            if (projectid >= 0)
            {
                query = query.Where(n => n.ProjectID == projectid);
            }
            if (!string.IsNullOrEmpty(member))
            {
                query = query.Where(w => w.Name == member).Select(s => s);
            }
            var test = query.Where(n => n.NotStarted != null && n.Underway != null && n.Suspend != null && n.Cancel != null && n.Pass != null
                && n.SeekHelp != null && n.PleaseExamine != null && n.Faield != null);
            return test;
        }


        public static IEnumerable<_Task> GetPassedTasks(int? projectid, string member, string taskName)
        {
            IQueryable<Task> tasks = from c in CH.DB.Tasks where c.TaskState.Code == 100 select c;
            IQueryable<_Task> results;
            if (projectid != null && projectid != 0)
            {
                tasks = from c in tasks where c.ProjectID == projectid select c;
            }
            if (!string.IsNullOrWhiteSpace(taskName))
            {
                tasks = tasks.Where(w => w.Name != null && w.Name.Contains(taskName));
            }

            if (Employee.CurrentRoleLVL != RoleLevel.Administrator)
            {
                tasks = tasks.Where(s => s.Project.IsActived);
            }

            results = from c in tasks

                      select new _Task()
                      {
                          ProjectCode = c.Project.ProjectCode,
                          CreatedDate = c.CreatedDate,
                          Creator = c.Creator,
                          Description = c.Description,
                          ID = c.ID,
                          ModifiedDate = c.ModifiedDate,
                          ModifiedUser = c.ModifiedUser,
                          Sequence = c.Sequence,
                          //Complement = c.Complement,
                          EndDate = c.EndDate,
                          Name = c.Name,
                          Owners = c.Owners,
                          ProjectID = c.ProjectID,
                          StartDate = c.StartDate,
                          Content = c.Content,
                          KMLinks = c.KMLinks,
                          TaskStateCode = c.TaskState != null ? c.TaskState.Code : 0,
                          Deliverables = c.Deliverables,
                          TaskStateID = c.TaskStateID,
                          FinishDate = c.FinishDate,
                          Assinger = c.Assinger,
                          TaskStateName = c.TaskState != null ? c.TaskState.Name : string.Empty,
                          TaskCode = c.TaskCode
                      };

            IEnumerable<_Task> result = null;



            if (!string.IsNullOrEmpty(member))
            {
                result = results.ToList().Where(w => w.Owners.Split(new string[] { ";" }, StringSplitOptions.RemoveEmptyEntries).Contains(member)).Select(s => s);
            }
            else
            {
                result = results.AsEnumerable();
            } return result;
        }

        internal static IEnumerable<_Task> GetNotifyTasks()
        {
            IQueryable<Task> tasks = from c in CH.DB.Tasks where !string.IsNullOrEmpty(c.Notify) && (c.TaskState.Code == _Task.InProgress || c.TaskState.Code == _Task.NeedApprove || c.TaskState.Code == _Task.Pass) select c;
            var user = Employee.CurrentUserName;


            tasks = from c in CH.DB.Tasks where c.Notify.Contains(user) select c;

            if (Employee.CurrentRoleLVL != RoleLevel.Administrator)
            {
                tasks = tasks.Where(s => s.Project.IsActived);
            }

            var taskslist = tasks.ToList().Where(w => w.Notify.Split(new string[] { ";", "；" }, StringSplitOptions.RemoveEmptyEntries).Contains(user)).Select(s => s);

            var results = from c in tasks

                          select new _Task()
                          {
                              Notify = c.Notify,
                              ProjectCode = c.Project.ProjectCode,
                              CreatedDate = c.CreatedDate,
                              Creator = c.Creator,
                              Description = c.Description,
                              ID = c.ID,
                              ModifiedDate = c.ModifiedDate,
                              ModifiedUser = c.ModifiedUser,
                              Sequence = c.Sequence,
                              //Complement = c.Complement,
                              EndDate = c.EndDate,
                              Name = c.Name,
                              Owners = c.Owners,
                              ProjectID = c.ProjectID,
                              StartDate = c.StartDate,
                              Content = c.Content,
                              KMLinks = c.KMLinks,
                              TaskStateCode = c.TaskState != null ? c.TaskState.Code : 0,
                              Deliverables = c.Deliverables,
                              TaskStateID = c.TaskStateID,
                              FinishDate = c.FinishDate,
                              Assinger = c.Assinger,
                              TaskStateName = c.TaskState != null ? c.TaskState.Name : string.Empty
                          };





            return results;
        }

        public static IEnumerable<_Task> GetAllCustomTasks(int? projectid, string member)
        {
            IQueryable<Task> tasks = from c in CH.DB.Tasks where c.TaskState.Code == -10 || c.TaskState.Code == -1 || c.TaskState.Code == 100 select c;
            IQueryable<_Task> results;
            if (projectid != null && projectid != 0)
                tasks = from c in tasks where c.ProjectID == projectid select c;

            if (Employee.CurrentRoleLVL != RoleLevel.Administrator)
            {
                tasks = tasks.Where(s => s.Project.IsActived);
            }

            results = from c in tasks

                      select new _Task()
                      {
                          ProjectCode = c.Project.ProjectCode,
                          CreatedDate = c.CreatedDate,
                          Creator = c.Creator,
                          Description = c.Description,
                          ID = c.ID,
                          ModifiedDate = c.ModifiedDate,
                          ModifiedUser = c.ModifiedUser,
                          Sequence = c.Sequence,
                          //Complement = c.Complement,
                          EndDate = c.EndDate,
                          Name = c.Name,
                          Owners = c.Owners,
                          ProjectID = c.ProjectID,
                          StartDate = c.StartDate,
                          Content = c.Content,
                          KMLinks = c.KMLinks,
                          TaskStateCode = c.TaskState != null ? c.TaskState.Code : 0,
                          Deliverables = c.Deliverables,
                          TaskStateID = c.TaskStateID,
                          FinishDate = c.FinishDate,
                          Assinger = c.Assinger,
                          TaskStateName = c.TaskState != null ? c.TaskState.Name : string.Empty
                      };

            IEnumerable<_Task> result = null;



            if (!string.IsNullOrEmpty(member))
            {
                result = results.ToList().Where(w => w.Owners.Split(new string[] { ";" }, StringSplitOptions.RemoveEmptyEntries).Contains(member)).Select(s => s);
            }
            else
            {
                result = results.AsEnumerable();
            } return result;
        }



        public static IEnumerable<_Task> GetNotProgressTasks(int? state)
        {
            IQueryable<Task> tasks = from c in CH.DB.Tasks select c;
            IQueryable<_Task> results;
            if (state != null && state != 0)
                tasks = from c in tasks where c.TaskState.Code == state select c;
            var user = Employee.CurrentRoleLVL;
            string roleName = Employee.GetRoleName(Employee.CurrentUserName);
            if (user > 100)
            {
                tasks = from c in CH.DB.Tasks where c.TaskState.Code != 30 select c;
            }
            else
            {
                tasks = from c in CH.DB.Tasks where c.TaskState.Code != 30 && c.Notify.Contains(roleName) select c;
            }

            if (Employee.CurrentRoleLVL != RoleLevel.Administrator)
            {
                tasks = tasks.Where(s => s.Project.IsActived);
            }

            results = from c in tasks

                      select new _Task()
                      {
                          ProjectCode = c.Project.ProjectCode,
                          CreatedDate = c.CreatedDate,
                          Creator = c.Creator,
                          Description = c.Description,
                          ID = c.ID,
                          ModifiedDate = c.ModifiedDate,
                          ModifiedUser = c.ModifiedUser,
                          Sequence = c.Sequence,
                          //Complement = c.Complement,
                          EndDate = c.EndDate,
                          Name = c.Name,
                          Owners = c.Owners,
                          ProjectID = c.ProjectID,
                          StartDate = c.StartDate,
                          Content = c.Content,
                          KMLinks = c.KMLinks,
                          TaskStateCode = c.TaskState != null ? c.TaskState.Code : 0,
                          Deliverables = c.Deliverables,
                          TaskStateID = c.TaskStateID,
                          FinishDate = c.FinishDate,
                          Assinger = c.Assinger,
                          TaskStateName = c.TaskState != null ? c.TaskState.Name : string.Empty
                      };

            IEnumerable<_Task> result = null;
            result = results.AsEnumerable();
            return result;
        }

        public static IEnumerable<_SimultaneousTeacher> GetSimultaneousTeacher(int? SimultaneousID)
        {
            IQueryable<SimultaneousTeacher> results = CH.DB.SimultaneousTeachers;
            if (SimultaneousID != null)
            {
                results = results.Where(s => s.SimultaneousID == SimultaneousID);
            }
            return from c in results
                   select new _SimultaneousTeacher()
                   {
                       SimultaneousID = c.SimultaneousID,
                       TeacherDesc = c.TeacherDesc,
                       TeacherMeet = c.TeacherMeet,
                       TeacherName = c.TeacherName,
                       CreatedDate = c.CreatedDate,
                       Creator = c.Creator,
                       Description = c.Description,
                       ID = c.ID,
                       ModifiedDate = c.ModifiedDate,
                       ModifiedUser = c.ModifiedUser,
                       Sequence = c.Sequence,
                   };
        }
    }
}